/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import DAO.Constantes;
import DAO.DAO_Revista;
import actionForm.ModificarRevistaActionForm;
import clases.Articulo;
import clases.Revista;
import clases.Seccion;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Rafa
 */
public class VerArticulosRevistaAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ModificarRevistaActionForm formulario = (ModificarRevistaActionForm) form;

        DAO_Revista daoRevista = new DAO_Revista();
        Revista revista = daoRevista.consultaRevistaPorId(Integer.parseInt(formulario.getIdRevista()), true);

        Set<Seccion> conjuntoSecciones = revista.getSeccions();
        Seccion[] listaTodasSecciones = conjuntoSecciones.toArray(new Seccion[]{});

        boolean enc = false;
        int i = 0;
        Seccion seccion = null;
        Integer idSec = Integer.parseInt(formulario.getIdSeccion());

        while ((i < listaTodasSecciones.length) && (enc == false)) {

            if (listaTodasSecciones[i].getIdSeccion().equals(idSec)) {
                enc = true;
                seccion = listaTodasSecciones[i];
            }
            i++;
        }
        if (seccion != null) {

            Set<Articulo> listaArticulosImprimir = seccion.getArticulos();
            Articulo[] conjuntoArticulosTotales = listaArticulosImprimir.toArray(new Articulo[]{});

            Set<Articulo> conjuntoArticulosImprimir = new HashSet();

            for (int j = 0; j < conjuntoArticulosTotales.length; j++) {

                if (conjuntoArticulosTotales[j].getEstadoArticulo().getEstado().equals(Constantes.getART_A_IMPRIMIR())) {
                    conjuntoArticulosImprimir.add(conjuntoArticulosTotales[j]);
                }
            }

            formulario.setListaArticulosImprimir(conjuntoArticulosImprimir);
            request.setAttribute("verArticulosRevista", conjuntoArticulosImprimir);
            return mapping.findForward(SUCCESS);
        } else {
            return mapping.findForward(FAILURE);
        }

    }
}
